<--- %%NOBANNER%% --> wordread.sas
 BackForward

/*-------------------<-- Start of Description-->---------------------\
| Read in bookmarked area from Microsoft word;                       |
| Note: a bookmark is required to use this function, and the         |
|       bookmark must cover the entire area that need to read in.    |
|       However, you don't have to open the word file when you want  |
|       to use this funciton.                                        |
|---------------------<-- End of Description-->----------------------|
|--------------------------------------------------------------------|
|------------<-- Start of Files or Arguments Needed-->---------------|
| Arguments:                                                         |
|    infile - the path and filename;                                 |
|    Selectbmark - the selection bookmark; the bookmark must cover   |
|                  the entire area that need to read in;             |
|    varfmt=var $20.|.., the variables and corresponding formats;    |
|    output=output data set name;                                    |
|-------------<-- End of Files or Arguments Needed-->----------------|
|--------------------------------------------------------------------|
|------------------<-- Start of Files Created-->---------------------|
| Example: %wordread(infile="J:\CLINICAL\TACHY\Studydoc\             |
|           Aced-af Project File\Center Readiness & Activation Lists\|
|           Center Readiness Lists\Final List of ACED.doc",          |
|           Selectionmark=sas,varfmt=centerid|invlname $40.|         |
|           invfname $40.| address $200., output=acedinvsite);       |
| Usage:   %wordread(infile=, selectbmark=t1,varfmt=. .|,output=);   |
\-------------------<-- End of Files Created-->---------------------*/
%macro wordread(infile=, selectbmark=t1,varfmt=. .|,output=);
/*--------------------------------------------\
| Author:   Duo Zhou;                         |
| Created:  9-25-2001 7:58pm;                 |
| Modified: 1-15-2002 7:45pm;                 |
| Purpose:  Read the contents of bookmark     |
|           covered area;                     |
\--------------------------------------------*/
options noxwait noxsync;
x 'Exit';
%local nvars npairs nfmts numi _i_ _j_ _k_ infile;
%let infile=%qscan(&infile,1,%str(''""));
%if (&infile ne) %then %do;
   %let dsid=%sysfunc(fileexist(&infile));
   %if &dsid %then %do;
      /*%put --> Note: Wait! The system is opening "&infile". ;*/
      /*%sysexec "&infile"; %wait(5);*/

      %let npairs=%words(&varfmt, dlm=%str(|,));
      %do _j_=1 %to &npairs;
         %let pair&_j_=%qscan(&varfmt,&_j_,%str(|,));
         %let var&_j_=%qscan(&&pair&_j_,1,%str( ));
         %let fmt&_j_=%qscan(&&pair&_j_,2,%str( ));
      %end;
      /*%let format1=%substr(&fmt1,2,%eval(%length(&fmt1)-1));*/
      filename longname dde "winword|&infile.!&selectbmark." notab;
      data &output;
         infile longname dlm='09'x dsd missover;
         format &var1 &fmt1;
         input %do _k_=1 %to &npairs;
                  &&var&_k_ 
               %if &&fmt&_k_ ne %then %do;
                     %if (%index(&&fmt&_k_,:)) %then %do;
                          &&fmt&_k_
                    %end;
                  %else %do;
                       :&&fmt&_k_
                  %end;
               %end;
              %end;;
      run;
      /*%wordclose;*/
   %end;
   %else %do;
      %put ==> Alert: File "&infile" doesn%str(%')t exist.;
   %end;
%end;
%else %do;
   %put ==> Alert: I need an input file.;
%end;
%mend wordread;